package co.fitstart.mobile.web.controller.credit;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.BusinessException;

import co.fitstart.entity.coupon.CreditCouponBatch;
import co.fitstart.entity.credit.Credit;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CreditCouponBatchService;
import co.fitstart.mobile.service.CreditService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.resolver.Secure;

@RequestMapping("/credit-coupons")
@Controller
public class CreditCouponBatchController {
    
    private static final Logger logger = LoggerFactory.getLogger(CreditCouponBatchController.class);
    
    @Autowired
    private CreditCouponBatchService creditCouponBatchService;
    
    @Autowired
    private CreditService creditService;
    
    /**
     * List all avaliable credit coupon
     * 
     * @param user 
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/products")
    @MobileDetect
    public ModelAndView list(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-coupon-list");
        
        List<CreditCouponBatch> creditCouponBatchs =  creditCouponBatchService.listAll();
        Credit credit = creditService.generallyLoad(user);
        
        modelAndView.addObject("creditCouponBatchs", creditCouponBatchs);
        modelAndView.addObject("credit", credit);
        
        return modelAndView;
    }
    
    /**
     * View creditCoupon detail
     * 
     * @param creditCouponId 
     * 
     * @return
     */
    @RequestMapping(value = "/{creditCouponId}", method = RequestMethod.GET)
    @Router(targetUrl = "/products")
    @MobileDetect
    public ModelAndView view(@PathVariable("creditCouponId")Long creditCouponId) {
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-coupon-view");
        
        CreditCouponBatch creditCouponBatch =  creditCouponBatchService.load(creditCouponId);
        modelAndView.addObject("creditCouponBatch", creditCouponBatch);
        
        return modelAndView;
    }
    
    
    /**
     * Do exchange credit coupon
     * 
     * @param user 
     * @param creditCouponId 
     * 
     * @return
     */
    @RequestMapping(value = "/{creditCouponId}/exchange", method = RequestMethod.POST)
    @Router(targetUrl = "/products")
    @MobileDetect
    public ModelAndView doExchange(@Secure User user, @PathVariable("creditCouponId")Long creditCouponId) {
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-coupon-exchange");
        
        try {
            
            Credit credit = creditService.generallyLoad(user);
            CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(creditCouponId);
            
            creditCouponBatchService.exchange(credit, creditCouponBatch);
            
            modelAndView.addObject("success", true);
            modelAndView.addObject("message", "兑换成功，请去下单页使用~");
            modelAndView.addObject("creditCouponBatchId", creditCouponId);
            
        } catch (BusinessException e) {
            
            modelAndView.addObject("success", false);
            modelAndView.addObject("message", e.getMessage());
            
        } catch (Exception e) {
            
            logger.error("#doExchange() faild", e);
            
            modelAndView.addObject("success", false);
            modelAndView.addObject("message", "兑换失败");
            
        }
        
        return modelAndView;
    }
    
}
